﻿@page "/GridToolbar"
@layout DataProviderAccessArea<INwindDataProvider>

<DemoPageSectionComponent Id="DataGrid-Toolbar" ShowHorizontalScrollBar="true">
    @inject NwindDataService NwindDataService
    
    @*BeginHide*@
    <div class="w-100">
        @*EndHide*@
        <DxDataGrid @ref="grid"
                    Data="@Data"
                    ShowFilterRow="@ShowFilterRow"
                    @bind-SingleSelectedDataRow="@SelectedRow"
                    RowInsertingAsync="@OnRowInsertingAsync"
                    RowUpdatingAsync="@OnRowUpdatingAsync"
                    RowRemovingAsync="@OnRowRemovingAsync"
                    InitNewRow="@OnInitNewRow">
            <HeaderTemplate>
                <DxToolbar ItemRenderStyleMode="ToolbarRenderStyleMode.Plain">
                    <DxToolbarItem Text="New" BeginGroup="true" Click="@OnAddNewClick" IconCssClass="grid-toolbar-new" />
                    <DxToolbarItem Text="Edit" Click="@OnEditClick" IconCssClass="grid-toolbar-edit" Enabled="@Enabled" />
                    <DxToolbarItem Text="Delete" Click="@OnDeleteClick" IconCssClass="grid-toolbar-delete" Enabled="@Enabled" />
                    <DxToolbarItem Text="Show Filter Row" BeginGroup="true" GroupName="FilterRow" Click="@OnShowFilterRow" IconCssClass="grid-toolbar-filter-row" />
                    <DxToolbarItem Text="Examples" NavigateUrl="https://supportcenter.devexpress.com/ticket/list/?preset=824230fc-7d61-47a7-a3f9-ea0eed42df56" BeginGroup="true"
                                   Alignment="ToolbarItemAlignment.Right" IconCssClass="grid-toolbar-document" target="_blank" />
                </DxToolbar>
            </HeaderTemplate>
            <Columns>
                <DxDataGridColumn Field="@nameof(Employee.FirstName)" />
                <DxDataGridColumn Field="@nameof(Employee.LastName)" />
                <DxDataGridColumn Field="@nameof(Employee.Title)" />
                <DxDataGridColumn Field="@nameof(Employee.TitleOfCourtesy)" />
                <DxDataGridDateEditColumn Field="@nameof(Employee.BirthDate)" />
                <DxDataGridDateEditColumn Field="@nameof(Employee.HireDate)" />
            </Columns>
        </DxDataGrid>
        @*BeginHide*@
    </div>
    @*EndHide*@

    @code {
        IEnumerable<EditableEmployee> Data;
        EditableEmployee SelectedRow;
        DxDataGrid<EditableEmployee> grid;
        bool Enabled = true;
        bool ShowFilterRow = false;

        protected override async Task OnInitializedAsync() {
            Data = await NwindDataService.GetEmployeesEditableAsync();
            SetSelection();
        }
        async Task OnAddNewClick() {
            await grid.StartRowEdit(null);
            if(!Enabled)
                ChangeToolbarEnabled(true);
        }
        async Task OnEditClick() {
            await grid.StartRowEdit(SelectedRow);
        }
        async Task OnDeleteClick() {
            await NwindDataService.RemoveEmployeeAsync(SelectedRow);
            Data = await NwindDataService.GetEmployeesEditableAsync();

            if(Data.Count() == 0)
                ChangeToolbarEnabled(false);
            else
                SetSelection();
            StateHasChanged();
        }
        async Task OnRowRemovingAsync(EditableEmployee dataItem) {
            await NwindDataService.RemoveEmployeeAsync(dataItem);
            Data = await NwindDataService.GetEmployeesEditableAsync();
            StateHasChanged();
        }
        async Task OnRowInsertingAsync(IDictionary<string, object> newValues) {
            await NwindDataService.InsertEmployeeAsync(newValues);
            Data = await NwindDataService.GetEmployeesEditableAsync();
            StateHasChanged();
        }
        async Task OnRowUpdatingAsync(EditableEmployee dataItem, IDictionary<string, object> newValues) {
            await NwindDataService.UpdateEmployeeAsync(dataItem, newValues);
            Data = await NwindDataService.GetEmployeesEditableAsync();
            StateHasChanged();
        }
        Task OnInitNewRow(Dictionary<string, object> values) {
            values.Add("FirstName", "First Name");
            values.Add("LastName", "Last Name");
            return Task.CompletedTask;
        }
        void SetSelection() {
            SelectedRow = Data.FirstOrDefault();
        }
        void ChangeToolbarEnabled(bool enabled) {
            Enabled = enabled;
            StateHasChanged();
        }
        void OnShowFilterRow(ToolbarItemClickEventArgs e) {
            ShowFilterRow = !ShowFilterRow;
        }
    }
</DemoPageSectionComponent>
